const config = require('../../../config/config.local');
const amazonMws = require('amazon-mws')(config.AWSAccessKeyId, config.AWSSecretKey);
const isProduction = process.env.NODE_ENV === 'production';

const getReportList = {};

getReportList.generateOption = function(seller_id, mws_auth_token, reportRequestIdList=null, reportTypeList=null, availableFromDate=null, availableToDate=null, maxCount=null){
  let baseOption = {
    'Version': '2009-01-01',
    'Action': 'GetReportList',
    'SellerId': seller_id,
    'MWSAuthToken': mws_auth_token,
  };
  if (availableFromDate)
    baseOption['AvailableFromDate'] = availableFromDate;
  if (availableToDate)
    baseOption['AvailableToDate'] = availableToDate;
  if (maxCount)
    baseOption['MaxCount'] = maxCount;

  if (reportRequestIdList) {
    let keyPrefix = "ReportRequestIdList.Id";
    reportRequestIdList.forEach((item, index) => {
      baseOption[keyPrefix + "." + (index+1)] = item
    });
  }
  if (reportTypeList) {
    let keyPrefix = "ReportTypeList.Type";
    reportTypeList.forEach((item, index) => {
      baseOption[keyPrefix + "." + (index+1)] = item
    });
  }

  return baseOption;
};

getReportList._ = function(options) {
  if (!isProduction) {
    return amazonMws.reports.search(options);
  } else {
    console.error("bot not implemented!")
  }
};

getReportList.getReportListAll = function(seller_id, mws_auth_token) {
 return this._(this.generateOption(seller_id, mws_auth_token, null, null, null, null, null));
};

module.exports = getReportList;